<template>
  <div
    class="positionInput"
    ref="positionInput"
    v-Inter="option"
    :style="{
      left: position.left + '%',
      top: position.top + '%',
    }"
  >
    <div class="selInput">
      <el-input
        size="small"
        class="minInput ignore"
        placeholder="请输入序号"
        v-model="orderNumber"
        @input="inputChange"
      ></el-input>

      <div class="arrowselinput" ref="arrowselinput">
        <el-tooltip
          effect="dark"
          content="标记名称不超过20字符"
          placement="top-start"
          :manual="true"
          v-model="errorTip"
        >
          <el-input
            size="small"
            class="largeInput ignore"
            v-model="orderText"
            @focus="handleFocus"
            @blur="handleBlur"
            @input="handleInput"
            placeholder="请输入内容"
          ></el-input>
        </el-tooltip>
        <i class="el-icon-arrow-down" ref="icon"></i>
        <!-- 内容 -->
        <div class="popverContent">
          <div
            v-for="(item, index) in arr"
            :key="index"
            class="textItem"
            @click="handleClick(item)"
          >
            <div class="left">{{ item.num }}</div>
            <div class="right">{{ item.text }}</div>
          </div>
        </div>
      </div>
    </div>
    <footer>
      <div class="leftFooter">
        <el-button type="text" @click="handleDel">删除</el-button>
      </div>
      <div class="right">
        <el-button size="mini" @click="$emit('inputFalse')">取消</el-button>
        <el-button
          size="mini"
          style="margin-left: 12px"
          type="primary"
          @click="ok"
          >确定</el-button
        >
      </div>
    </footer>
  </div>
</template>

<script>
export default {
  props: {
    position: {
      type: Object,
      default: () => {
        return {
          left: 0,
          top: 0,
        };
      },
    },
    scale: {
      type: Number,
      default: 1,
    },
  },
  data() {
    return {
      orderNumber: "",
      orderText: "",
      popoverShow: false,
      arr: [
        { num: "1", text: "12121312" },
        { num: "112", text: "123" },
        { num: "12", text: "121312" },
        { num: "154", text: "1" },
      ],
      errorTip: false,
      option: {
        // pardom: ".mask",
        ignore: ".ignore",
        cb: () => {
          this.popoverShow = false;
        },
        scale: 1,
      },
    };
  },
  watch: {
    popoverShow: {
      handler(nv, ov) {
        if (nv) {
          this.$refs.icon.style.transform = "rotate(180deg)";
        } else {
          this.$refs.icon.style.transform = "rotate(0deg)";
        }
      },
    },
  },
  mounted() {
    this.option.scale = this.scale;
    document.addEventListener("click", this.closePopver);
    this.$refs.positionInput.onmousedown = (e) => {
      e.stopPropagation();
    };
  },
  methods: {
    // 确定事件
    ok() {
      this.$emit("ok", {
        orderNumber: this.orderNumber,
        orderText: this.orderText,
      });
    },
    //删除事件
    handleDel() {
      this.$emit("del", "111111");
    },
    handleFocus() {
      this.$refs.arrowselinput.style.borderColor = "#409EFF";
      this.popoverShow = true;
    },
    handleBlur() {
      this.$refs.arrowselinput.style.borderColor = "#dcdfe6";
    },
    handleInput() {
      if (this.orderText.length > 20) {
        this.orderText = this.orderText.substring(0, 20);
        this.errorTip = true;
      } else {
        this.errorTip = false;
      }
    },
    handleClick(item) {
      if (!this.orderNumber) {
        this.orderNumber = item.num;
      }
      this.orderText = item.text;
    },
    closePopver(e) {
      let dom = this.$refs.arrowselinput;
      if (dom && !dom.contains(e.target)) {
        this.popoverShow = false;
      }
    },
    inputChange(val) {},
  },
  beforeDestroy() {
    document.removeEventListener("click", this.closePopver);
  },
};
</script>

<style lang="less" scoped>
.positionInput {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 363px;
  height: 86px;
  background: #ffffff;
  box-shadow: 0px 2px 10px 0px rgba(88, 99, 125, 0.65);
  border-radius: 2px 2px 2px 2px;
  padding: 10px;
  box-sizing: border-box;
  z-index: 999999999;
}
.selInput {
  display: flex;
  align-items: center;
  .minInput {
    width: 100px;
  }
  /deep/ .minInput .el-input__inner {
    width: 100px;
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
  }
  & > span {
    flex: 1;
    position: relative;
  }
}
.arrowselinput {
  display: flex;
  align-items: center;
  border: 1px solid #dcdfe6;
  box-sizing: border-box;
  flex: 1;
  height: 32px;
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
  padding-right: 6px;
  i {
    transition: 0.3s;
    cursor: pointer;
  }
}
/deep/ .largeInput .el-input__inner {
  border: none;
  height: 28px;
  line-height: 28px;
}
footer {
  display: flex;
  justify-content: space-between;
  .right {
    display: flex;
    align-items: center;
  }
}

.popverContent {
  position: absolute;
  background-color: #fff;
  width: 242px;
  padding: 12px;
  box-sizing: border-box;
  max-height: 200px;
  overflow: auto;
  top: 48px;
  right: 10px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  box-sizing: border-box;
  border: 1px solid red;
}
.textItem {
  display: flex;
  justify-content: space-between;
  line-height: 30px;
  height: 30px;
  cursor: pointer;
  &:hover {
    background-color: #f2f4f9;
  }
  .left {
    width: 60px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .right {
    flex: 1;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
</style>
<style>
.imgPopver {
  padding: 0;
  z-index: 999999999999999 !important;
}
</style>
